package com.example.smartcity.ui.notifications;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;


import com.example.smartcity.R;
import com.example.smartcity.ui.Adapter.NewsViewPagerAdapter;
import com.example.smartcity.ui.Bean.NewsBean;
import com.example.smartcity.ui.Bean.NewsKind;
import com.example.smartcity.ui.Tools.OkHttpUtils;
import com.example.smartcity.ui.home.Fragment.f1;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.google.gson.Gson;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class NotificationsFragment extends Fragment {

    private Banner banner;
    private TabLayout tabLayout;
    private ViewPager2 viewPager2;
    ArrayList<Integer> list = new ArrayList<>();
    private String TAG = "TAG";

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_notifications, container, false);
        banner = root.findViewById(R.id.banner);
        tabLayout =  root.findViewById(R.id.tabLayout);
        viewPager2 =  root.findViewById(R.id.ViewPager2);
        BannerView();
        INitNews();
        return root;
    }
    public void BannerView(){
        list.add(R.drawable.news1);
        list.add(R.drawable.news2);
        banner.setAdapter(new BannerImageAdapter<Integer>(list) {
            @Override
            public void onBindView(BannerImageHolder bannerImageHolder, Integer integer, int i, int i1) {
                bannerImageHolder.imageView.setImageResource(integer);
            }
        }).setIndicator(new CircleIndicator(NotificationsFragment.this.getContext())).start();
    }

    public void INitNews(){
        ArrayList<Integer> dictCodeList = new ArrayList<>();
        String NewsKindUrl = OkHttpUtils.getBaseUrl(NotificationsFragment.this.getContext()) + "/system/dict/data/type/press_category";
        OkHttpUtils.getCall(NewsKindUrl).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d(TAG, "onFailure: "+e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                ArrayList<String> NewsKindList = new ArrayList<>();
                ArrayList<NewsKind.DataBean> tempList = new ArrayList<>();
                NewsKind bean = new Gson().fromJson(response.body().string(),NewsKind.class);
                int code = bean.getCode();
                if (code==200){
                    List<NewsKind.DataBean> dataBeans = bean.getData();
                    if (dataBeans!=null){
                        tempList.addAll(dataBeans);
                    }
                    tabLayout.post(new Runnable() {
                        @Override
                        public void run() {
                            for (int i = 0; i < tempList.size(); i++) {
                                NewsKindList.add(tempList.get(i).getDictLabel());
                                dictCodeList.add(tempList.get(i).getDictCode());
                            }
                            initNewsMessage(dictCodeList,NewsKindList);
                        }
                    });
                }
            }
        });
    }

    public void initNewsMessage(ArrayList<Integer> dictCodeList,ArrayList<String> NewsKindList){
        new Thread(new Runnable() {
            @Override
            public void run() {
                List<Fragment> fragments = new ArrayList<>();
                //每个Fragment中的信息组
                ArrayList<List<NewsBean.RowsBean>> list = new ArrayList<>();
                for (int i = 0; i < dictCodeList.size(); i++) {
                    String NewsUrl = OkHttpUtils.getBaseUrl(NotificationsFragment.this.getContext())+"/press/press/list?pageNum=1&pageSize=10&pressCategory="+dictCodeList.get(i);
                    String json = null;
                    try {
                        json = OkHttpUtils.getCall(NewsUrl).execute().body().string();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    NewsBean bean = new Gson().fromJson(json,NewsBean.class);
                    if (bean!=null){
                        List<NewsBean.RowsBean> tempList = bean.getRows();
                        if (bean.getCode()==200){
                            if (tempList!=null){
                                list.add(tempList);
                            }
                        }
                    }

                }
                //初始化Fragment
                for (int i = 0; i < 6; i++) {
                    fragments.add(f1.newInstance(list.get(i)));
                }
                //配置适配器
                NewsViewPagerAdapter adapter = new NewsViewPagerAdapter(getChildFragmentManager(),getLifecycle(),fragments);
                viewPager2.post(new Runnable() {
                    @Override
                    public void run() {
                        viewPager2.setAdapter(adapter);
                        new TabLayoutMediator(tabLayout,viewPager2, new TabLayoutMediator.TabConfigurationStrategy() {
                            @Override
                            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                                //设置TabLayout的标签
                                tab.setText(NewsKindList.get(position));
                            }
                        }).attach();
                    }
                });
            }
        }).start();

    }

}